//
//  StopWatchSONViewController.m
//  StopWatchSON
//
//  Created by bkha1 on 10/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "StopWatchSONViewController.h"

@implementation StopWatchSONViewController
@synthesize stopWatchLabel;

- (void)dealloc
{
    [stopWatchLabel release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [self setStopWatchLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)onStartPressed:(id)sender {
    //stopWatchLabel.text = @"START PRESSED";
    startDate = [[NSDate date]retain];
    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 target: self selector: @selector(updateTimer) userInfo:nil repeats:YES];
}

- (IBAction)onStopPressed:(id)sender {
    //stopWatchLabel.text = @"STOP PRESSED";
    [stopWatchTimer invalidate];
    stopWatchTimer = nil;
    [self updateTimer];
}

- (void) updateTimer
{
    //static NSInteger counter = 0;
    //stopWatchLabel.text = [NSString stringWithFormat:@"Counter: %i", counter++];
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss.SSS"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    NSString *timeString=[dateFormatter stringFromDate:timerDate];
    stopWatchLabel.text = timeString;
    [dateFormatter release];
}

/*- (IBAction)onStartPressed:(id)sender{
    startDate = [[NSDate date]retain];
    
    stop
}*/
@end
